/**
 * Created by JetBrains PhpStorm.
 * User: taoqili
 * Date: 12-2-20
 * Time: ����11:19
 * To change this template use File | Settings | File Templates.
 */

(function(){

    $(function(){
        var parents = window.parent.document;

        //检验视频地址
        $(parents).find('#edui14').on('click',function(){
            var key = $('#videoUrl').val();
            initVideo(key);
        });

        //请求视频地址
        function initVideo(key){
            var defaultTxt = '<span><i class="fa fa-spinner fa-spin"></i>正在解析…</span>',
                errorTxt = function(txt){
                    return '<span class="red"><i class="fa fa-exclamation-circle"></i>'+txt+'</span>';
                }
            $('#videoTab .state').html(defaultTxt);
            $.ajax({
                url:'http://helper.kdnet.net/api/video/parsingByKeyword',//'http://special.kdnet.net/video/get_video.php',
                dataType: 'jsonp',
                jsonp:'callback',
                data:{
                    keyword: key,
                },
                success: function(json){
                    if(json.success){
                        console.log(json);
                        var item = json.data,
                            txt = '<p style="text-align: center;"><embed class="c-video" src="'+item.flash+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="opaque"'+
                                ' data-link="'+item.link+'"' +
                                ' data-platform="'+item.platform+'"' +
                                ' data-thumbnail="'+item.TCthumb+'"' +
                                ' data-title="'+item.title+'"' +
                                '></embed></p>';
                        editor.execCommand('inserthtml', txt);
                        $(parents).find('#edui12,#edui6').hide();
                    }else {
                        $('#videoTab .state').html(errorTxt('我们无法识别你输入的链接地址。'));
                    }
                },
                error: function(json){
                    $('#videoTab .state').html(errorTxt(json.message));
                }
            })
        }
    })


})();